﻿using System;
using System.Collections.Generic;
using System.Linq;
using ETL.Business.Models;
using ETL.Common.Logging;
using ETL.DataAccess;
using ETL.Entity;

namespace ETL.Business.Handlers.ProjectETRHandler
{
    public class GetProjectETR
    {
        private readonly ProjectETRRepository _projectEtrRepository;

        public GetProjectETR(ProjectETRRepository projectEtrRepository)
        {
            _projectEtrRepository = projectEtrRepository;
        }
        public GetProjectETR()
        {
            _projectEtrRepository = new ProjectETRRepository();
        }

        //Return a Project ETR with all column
        public virtual ProjectETRModel Execute(String etr_ref)
        {
            ProjectETRModel projectEtrModel = null;

            // try to get from db
            try
            {
                project_etr projectEtrData = _projectEtrRepository.GetProjectETRbyRef(etr_ref);
                // Parse to Business Model
                if (projectEtrData != null)
                {
                    projectEtrModel = new ProjectETRModel(projectEtrData);
                }
            }
            catch (Exception ex)
            {
                LogService.Service.Debug(ex);
            }
            
            return projectEtrModel;

        }

        //Return all Project ETR Model which contains 3 fiels as in the repository
        public virtual IEnumerable<ProjectETRModel> Execute()
        {
            IEnumerable<project_etr> projectEtrData = null;

            // try to get from db
            try
            {
                projectEtrData = _projectEtrRepository.GetProjectETR();
            }
            catch (Exception ex)
            {
                LogService.Service.Debug(ex);
            }

            //Convert project_etr to ProjectETRModel
            var projectEtrModel = from prjEtr in projectEtrData
                                  select new ProjectETRModel(prjEtr);
            return projectEtrModel;

        }

    }
}
